package org.hang.util;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 内容审核工具类
 */

public class BaiduAiUtils {

    //设置APPID/AK/SK
//    public static final String APP_ID = "52572862";
//    public static final String API_KEY = "vmTMq28huTcQ1XdIyucvGNTo";
//   public static final String SECRET_KEY = "rt4052P8JaARmJQKrbi0V37WKzViXrZE";
    public static final String APP_ID = "52506311";
    public static final String API_KEY = "JHDnrIUPDJbI8qzSVR8nSr1r";
    public static final String SECRET_KEY = "ewoXPL4yUWw8Rl3xTRcoAMPJxjFKHpgI";

    //设计成单例，只有一个AipContentCensor 对象
    private static  AipContentCensor client;
    static{
        // 初始化一个AipContentCensor
        client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
    }

    /**
     * @param text 要审核文本
     * @return 审核结果  false代表不合规  true 合规
     */

    public static boolean textReview(String text) {
//        JSONObject response = client.textCensorUserDefined(text);
//        System.out.println(response.toString());

        try {
            //对内容进行审核
            JSONObject response = client.textCensorUserDefined(text);
            //conclusionType:审核结果类型，可取值1.合规，2.不合规，3.疑似，4.审核失败
            int conclusionType = response.getInt("conclusionType");
            return  conclusionType==1? true:false;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * @param url 要审核图像网络地址
     * @return 审核结果  false代表不合规  true 合规
     */
    public static boolean imgReview(String  url) {

        try {
            // 参数为url
            JSONObject response = client.imageCensorUserDefined(url, EImgType.URL, null);
            int conclusionType = response.getInt("conclusionType");
            System.out.println(response.toString());
            return  conclusionType==1? true:false;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {

        System.out.println(textReview("好好学习就能变富"));
    }

}
